package com.example.myrecycle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class StarDecoration extends RecyclerView.ItemDecoration {
    private int groupHeadHeight;
    private Context context;
    private Paint mPaint;
    private Paint headPaint;
    public StarDecoration(Context context) {
        this.context = context;
        groupHeadHeight = dp2px(context,100);
        mPaint = new Paint();
        headPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(50);
        headPaint.setColor(Color.RED);
    }


    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (parent.getAdapter() instanceof StarAdapter){
            StarAdapter starAdapter = (StarAdapter) parent.getAdapter();
            //当前屏幕的item个数
            int count = parent.getChildCount();
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            for (int i = 0; i < count; i++) {
                View view = parent.getChildAt(i);
                int position = parent.getChildLayoutPosition(view);
                //是头部
                if(starAdapter.isGroupHead(position) && view.getTop() - parent.getPaddingTop() - groupHeadHeight>=0){
                    c.drawRect(left, view.getTop()-groupHeadHeight,right,view.getTop(),headPaint);
                    c.drawText(starAdapter.getGroupName(position),left+10,view.getTop()-groupHeadHeight/2+10,
                            mPaint);
                }
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if(parent.getAdapter() instanceof StarAdapter){
            StarAdapter starAdapter = (StarAdapter) parent.getAdapter();
            int left = parent.getPaddingLeft();
            int top = parent.getPaddingTop();
            int right = parent.getWidth() - parent.getPaddingRight();
            int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
            //获取对应VIew
            View view = parent.findViewHolderForAdapterPosition(position).itemView;
            boolean isGroupHead = starAdapter.isGroupHead(position+1);

            if (isGroupHead){
                //如果屏幕上显示的第一个View的下一个View是组头的话
                //即将推上去
                int bottom = Math.min(groupHeadHeight,view.getBottom() - top);
                c.drawRect(left,top,right,top+bottom,headPaint);
                c.drawText(starAdapter.getGroupName(position),left+10,top + bottom - groupHeadHeight/2+10,mPaint);
            }else{
                //如果屏幕上显示的第一个View的下一个View不是组头的话，就画一个head一直显示在bar下面
                c.drawRect(left,top,right,top+groupHeadHeight,headPaint);
                c.drawText(starAdapter.getGroupName(position),left+10,top+groupHeadHeight/2+10,mPaint);
            }
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if(parent.getAdapter() instanceof StarAdapter){
            StarAdapter starAdapter = (StarAdapter) parent.getAdapter();
            int position = parent.getChildLayoutPosition(view);
            boolean isGroupHead = starAdapter.isGroupHead(position);
            if(isGroupHead){
                outRect.set(0,groupHeadHeight,0,0);
            }else {
                outRect.set(0,2,0,0);
            }
        }

    }
    private int dp2px(Context context,float dpValue){
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue *scale * 0.5f);
    }
}
